package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.goods.domain.TGoodsInfo;
import com.ruoyi.goods.service.ITGoodsInfoService;
import com.ruoyi.statistic.domain.WMonthEnterWarehouseStat;
import com.ruoyi.statistic.domain.WStatisticDimensionBigit;
import com.ruoyi.statistic.service.IWMonthEnterWarehouseGoodsRelService;
import com.ruoyi.statistic.service.IWMonthEnterWarehouseStatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.Date;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author wzy
 */
@Component("mnTask")
public class MnTask
{
    @Autowired
    private ITGoodsInfoService tGoodsInfoService;

    @Autowired
    private IWMonthEnterWarehouseStatService wMonthEnterWarehouseStatService;

    @Autowired
    private IWMonthEnterWarehouseGoodsRelService wMonthEnterWarehouseGoodsRelService;

    private static Logger logger = LoggerFactory.getLogger(MnTask.class);

    /**
     * 统计月入库量，无参，按入库客户，货物类型，维度统计
     */

    public void statisticMonthEnterWarehouseTotal(String strEnterWarehouseDate)
    {
        logger.debug("统计，月入库量，入库客户，货物类型，开始 ...");
        /**
         * 计算当前日期，前一月日期
         */
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate currentDate = LocalDate.now();
        if(StringUtils.isNotEmpty(strEnterWarehouseDate)) {
            currentDate = LocalDate.parse(strEnterWarehouseDate, formatter);
        }
        logger.debug("当前日期: " + currentDate);

        LocalDate lastMonthDate = currentDate.minusMonths(1);
        logger.debug("上月日期: " + lastMonthDate);
        LocalDate lastMonthFirstDay = lastMonthDate.with(TemporalAdjusters.firstDayOfMonth());
        logger.debug("上个月第一天：" + lastMonthFirstDay);
        LocalDate lastMonthLastDay = lastMonthDate.with(TemporalAdjusters.lastDayOfMonth());
        logger.debug("上个月最后一天：" + lastMonthLastDay);

        TGoodsInfo tGoodsInfo = new TGoodsInfo();

        Double enterWarehouseWeightTotal = 0.0, enterWarehouseTheoryWeightTotal = 0.0;

        WMonthEnterWarehouseStat wMonthEnterWarehouseStat = new WMonthEnterWarehouseStat();

        String monthEnterStatisticNumber;

        Long monthExitStatisticId = 0L;

        List<Long> goodsIds = new ArrayList<>();
        /**

        /**
         * 按入库日期，筛选入库客户，货物类型，列表
         */
        List<WStatisticDimensionBigit> statisticDimensionBigits = tGoodsInfoService.selectStatisticDimensionByEnterWarehouseDate(Date.valueOf(lastMonthFirstDay), Date.valueOf(lastMonthLastDay));
        //logger.debug("statisticDimensionBigits: " + statisticDimensionBigits);
        //logger.debug("statisticDimensionBigits: " + (statisticDimensionBigits instanceof Map) );
        //logger.debug("statisticDimensionBigits: " + (statisticDimensionBigits instanceof List) );

        /**
         * 遍历合约号，货物
         */
        for(WStatisticDimensionBigit statisticDimensionBigit : statisticDimensionBigits)
        {

            /**
             * 按入库客户，货物类型。入库日期，查询货物
             */
            tGoodsInfo.setEnterWarehouseCustomer(statisticDimensionBigit.getCustomer());
            tGoodsInfo.setGoodsType(statisticDimensionBigit.getGoodsType());
            //tGoodsInfo.setExitWarehouseTime(Date.valueOf(yesterday));
            //logger.debug("OOO " + "statisticDayExitWarehouseTotal = " + tGoodsInfo);

            List<TGoodsInfo> goods = tGoodsInfoService.selectTGoodsInfoListUsedByEnterStatistic(tGoodsInfo, Date.valueOf(lastMonthFirstDay), Date.valueOf(lastMonthLastDay));
            /**
             * 合计入库货物重量，记录货物ID
             */
            for(TGoodsInfo good : goods)
            {
                enterWarehouseWeightTotal = enterWarehouseWeightTotal + good.getWeight();
                enterWarehouseTheoryWeightTotal = enterWarehouseTheoryWeightTotal + good.getTheoryWeight();
                goodsIds.add(good.getGoodsId());
            }

            /**
             * 保护，goodsIds，为空，直接退入，本次循环
             */
            if(goodsIds.isEmpty()) break;

            /**
             * 月入库量编号
             */
            monthEnterStatisticNumber = OrderUtils.getOrderNumber("MX", "-");

            /**
             * 入库重量，四舍五入
             */
            enterWarehouseWeightTotal = Math.round(enterWarehouseWeightTotal * 1000.0) / 1000.0;
            enterWarehouseTheoryWeightTotal = Math.round(enterWarehouseTheoryWeightTotal * 1000.0) / 1000.0;

            /**
             * 月入库量，数据准备
             */
            wMonthEnterWarehouseStat.setMonthEnterStatisticNumber(monthEnterStatisticNumber);
            wMonthEnterWarehouseStat.setEnterWarehouseDate(Date.valueOf(lastMonthDate));
            wMonthEnterWarehouseStat.setEnterWarehouseCustomer(statisticDimensionBigit.getCustomer());
            wMonthEnterWarehouseStat.setGoodsType(statisticDimensionBigit.getGoodsType());
            wMonthEnterWarehouseStat.setWeightMode(statisticDimensionBigit.getWeightMode());
            wMonthEnterWarehouseStat.setMonthEnterWarehouseWeight(enterWarehouseWeightTotal);
            wMonthEnterWarehouseStat.setMonthEnterWarehouseTheoryWeight(enterWarehouseTheoryWeightTotal);
            wMonthEnterWarehouseStat.setCreateBy("MnTask");
            wMonthEnterWarehouseStat.setCreateTime(DateUtils.getNowDate());

            /**
             * 月入库量，插入数据
             */
            wMonthEnterWarehouseStatService.insertWMonthEnterWarehouseStat(wMonthEnterWarehouseStat);
            //sleep(300);
            /**
             * 获取插入记录，ID
             */
            monthExitStatisticId = wMonthEnterWarehouseStat.getMonthEnterStatisticId();
            //logger.debug("OOO " + "monthExitStatisticId = " + monthExitStatisticId);

            /**
             * enterWarehouseWeightTotal清零
             */
            enterWarehouseWeightTotal = 0.0;
            enterWarehouseTheoryWeightTotal = 0.0;

            /**
             * 入入库量关联表，数据插入
             */
            //logger.debug("OOO " + "goodsIds = " + goodsIds);
            //wMonthEnterWarehouseGoodsRelService.insertWMonthEnterWarehouseGoodsRelExt(monthExitStatisticId, goodsIds);
            goodsIds.clear();

        }

        logger.debug("统计，月入库量，入库客户，货物类型，结束 ...");
    }

}
